# -*- coding: utf-8 -*-
# @Time : 2021/2/7 15:56
# @Author : zhangcheng
# @Software: PyCharm

from models.user_model import User
from init.init import dbsession
from sqlalchemy import func, and_
from utils.commons import query_to_dict


class UserController(User):
    @classmethod
    # # 创建用户，需要传入用户名name和密码password
    def add(cls, **kwargs):
        try:
            # 获得最大的autoid
            max_id = dbsession.query(func.max(cls.autoid)).first()
        except Exception as e:
            return {"message": "数据库异常，生成ID失效", "error": e.args}

        if max_id[0] is None:
            id = 1
        else:
            id = max_id[0] + 1
        try:
            user = User(autoid=id, name=kwargs.get("name", ""), password=kwargs.get("password"))
            dbsession.add(user)
            dbsession.commit()
        except Exception as e:
            dbsession.rollback()
            return {"message": "数据库异常，生成用户失效", "error": e.args}
        result = query_to_dict(user)
        return {"message": "添加成功", "data": result}

    # 根据autoid查询，一次一条,或根据name查询，一次一个列表
    @classmethod
    def get(cls, **kwargs):
        try:
            if kwargs.get("autoid"):
                user_info = dbsession.query(User).filter_by(autoid=kwargs.get("autoid")).first()
            if kwargs.get("name"):
                user_info = dbsession.query(User).filter_by(name=kwargs.get("name")).all()
            if not user_info:
                return {"message": "无用户信息", "error": "no data"}
            result = query_to_dict(user_info)
        except Exception as e:
            return {"message": "数据库异常，获取数据失败", "error": e.args}
        return {"message": "查询成功", "data": result}


    # 根据autoid删除，一次一条
    @classmethod
    def delete(cls, **kwargs):
        # 先查询判断记录是否存在
        try:
            user_info = dbsession.query(User).filter_by(autoid=kwargs.get("autoid")).first()
        except Exception as e:
            return {"message": "数据库异常，信息查询失败", "error": e.args}
        if not user_info:
            return {"message": "无用户信息", "error": "no data"}
        try:
            dbsession.delete(user_info)
            dbsession.commit()
            result = query_to_dict(user_info)
            return {"message": "删除成功", "data": result}
        except Exception as e:
            dbsession.rollback()
            return {"message": "数据库异常，删除失败", "error": e.args}

    # 根据autoid更新，一次一条
    @classmethod
    def update(cls, **kwargs):
        # 先查询判断记录是否存在
        try:
            user_info = dbsession.query(User).filter_by(autoid=kwargs.get("autoid")).first()
        except Exception as e:
            return {"message": "数据库异常，信息查询失败", "error": e.args}
        if not user_info:
            return {"message": "无用户信息", "error": "no data"}
        old_data = query_to_dict(user_info)
        try:
            dbsession.query(User).filter_by(autoid=kwargs.get("autoid")).update(kwargs)
            dbsession.commit()
            new_user_info = dbsession.query(User).filter_by(autoid=kwargs.get("autoid")).first()
            new_data = query_to_dict(new_user_info)
            return {"message": "更新成功", "old_data": old_data, "new_data": new_data}
        except Exception as e:
            dbsession.rollback()
            return {"message": "数据库异常，更新失败", "error": e.args}

